package com.example.itshome.network;

import android.app.Activity;
import android.util.Log;
import android.widget.TextView;
import com.example.itshome.adapter.CommonRecycleViewAdapter;
import com.example.itshome.entity.CommonEntity;
import com.example.itshome.manager.UserInfoManager;
import com.example.itshome.util.ToastUtil;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class SendCommonRequest extends AbstractNetworkRequest{

    private String message;
    private int circleId;
    private CommonRecycleViewAdapter adapter;
    private Runnable run;
    private TextView commonCountTextView;

    public SendCommonRequest setCommonCountTextView(TextView commonCountTextView) {
        this.commonCountTextView = commonCountTextView;
        return this;
    }

    public SendCommonRequest setRun(Runnable run) {
        this.run = run;
        return this;
    }

    public SendCommonRequest setMessage(String message) {
        this.message = message;
        return this;
    }

    public SendCommonRequest setCircleId(int circleId) {
        this.circleId = circleId;
        return this;
    }

    public SendCommonRequest setAdapter(CommonRecycleViewAdapter adapter) {
        this.adapter = adapter;
        return this;
    }

    public SendCommonRequest(Activity activity) {
        super(activity);
    }

    @Override
    protected String getPathAndParams() {
        return "moment/post-comment?username="+ UserInfoManager.getInstance().getUser().getUsername()+
                "&id="+circleId+
                "&comment="+message;
    }

    @Override
    protected void handleResult(String result) {
        try {
            JSONObject jsonObject=new JSONObject(result);
            int code= jsonObject.getInt("code");
            if(code!=200){
                return;
            }
            setHandleResultRunnable(new Runnable() {
                @Override
                public void run() {
                    ToastUtil.showToast("发送成功",activity);
                    run.run();
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CHINA);
                    adapter.addItem(new CommonEntity()
                            .setHeader(UserInfoManager.getInstance().getUser().getHeaderSrc())
                            .setMessage(message)
                            .setNickname(UserInfoManager.getInstance().getUser().getNickname())
                            .setTime(sdf.format(new Date())));
                    adapter.notifyItemInserted(0);
                    String s= commonCountTextView.getText().toString();
                    int i =s.indexOf("条");
                    s=s.substring(1,i);
                    i=Integer.parseInt(s)+1;
                    commonCountTextView.setText("共"+i+"条评论");
                }
            });
        }catch (JSONException e){
            e.printStackTrace();
        }
    }

    @Override
    protected boolean canStart() {
        return !message.trim().equals("");
    }
}
